package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.untils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validate")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;
    /*
    * 手机验证码发送
    * */
    @RequestMapping("/orderCode")
    public Result orderCode(String telephone){
        try {
            //生成一个4位数验证码
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            //发送验证码
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            //把生成的验证码保存到缓存中 保存5分钟
            System.out.println(code);
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,code.toString());
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
